<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\model\Assets;
use app\common\model\Bank;
use app\common\model\Order;
use app\common\model\Relation;
use app\common\model\UserSign;
use think\exception\ValidateException;
use think\facade\View;
use app\common\model\User as UserModel;

class User extends Base
{
    public function index()
    {
        View::assign([
            'assets' => Assets::getFields(3),
            'system' => $this->system,
            'order_1' => Order::where(['uid' => $this->user->id, 'status' => 1, 'is_del' => 0])->count(),
            'order_2' => Order::where(['uid' => $this->user->id, 'status' => 2, 'is_del' => 0])->count(),
            'order_3' => Order::where(['uid' => $this->user->id, 'status' => 3, 'is_del' => 0])->count(),
        ]);

        return View::fetch();
    }

    public function setting()
    {
        return View::fetch();
    }

    public function personal()
    {
        return View::fetch();
    }

    public function edit()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param('', '', $this->filter);
            UserModel::update($param, ['id' => $this->user->id]);

            return json(['code' => 1, 'msg' => '已修改']);
        }

        return View::fetch();
    }

    public function method_list()
    {
        return View::fetch();
    }

    public function method($type)
    {
        View::assign([
            'type' => $type,
            'title' => get_pay_way($type),
            'bank_list' => Bank::where(['status' => 1, 'is_del' => 0])->select()
        ]);

        return View::fetch();
    }

    public function method_save($type)
    {
        try {
            $param = $this->request->post('', '', $this->filter);

            $v = $type == 'bank' ? 'MethodBank' : 'MethodImg';
            try {
                $this->validate($param, $v);
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            switch ($type) {
                case "bank":
                    break;
                default:
                    $param[$type . '_name'] = $param['name'];
                    $param[$type . '_image'] = $param['image'];
                    unset($param['name']);
                    unset($param['image']);
            }

            UserModel::update($param, ['id' => $this->user->id]);

            return json(['code' => 1, 'msg' => '已保存', 'url' => $param['go_back'] ? url('draw/index')->build() : '']);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function my_team()
    {
        if ($this->request->isAjax()) {
            $page = $this->request->param('page', 1);
            $limit = 20;
            $that = Relation::alias('a')->leftJoin('user b', 'a.new_uid = b.id')
                ->field('b.id,b.code,b.nickname,b.mobile,b.photo,b.team_total,a.algebra')
                ->where(['a.uid' => $this->user->id, 'b.is_del' => 0])
                ->order('a.algebra asc, b.id desc')->page(intval($page), $limit);
            $count = $that->count();
            $list = $that->select();
            $allPage = intval(ceil($count / $limit));
            foreach ($list as $val) {
                $val->mobile = hideMobile($val->mobile);
            }

            return json(['code' => 1, 'data' => $list, 'pages' => $allPage, 'count' => $count]);
        }
        $team = Relation::alias('a')->leftJoin('user b', 'a.new_uid = b.id')
            ->where(['a.uid' => $this->user->id, 'b.is_del' => 0])->count('b.id');
        View::assign(['team' => $team]);

        return View::fetch();
    }

    public function share()
    {
        $url = $this->request->domain() . "/index/register/index.html?code=" . $this->user->code;
        View::assign('url', $url);

        return View::fetch();
    }

    public function logout()
    {
        $token = md5(time() . $this->user['id'] . $this->request->ip());

        UserModel::update([
            'token' => $token
        ], ['id' => $this->user['id']]);
        cookie('token', null);

        return json(['code' => 1, 'msg' => '已退出登录', 'url' => url('login/index')->build()]);
    }

    public function sign()
    {
        $date = $this->request->get('date') ?: date('Y-m');
        $choice_time = strtotime($date);
        $now_time = strtotime(date('Y-m'));
        //获取某月天数
        $day = date("t", $choice_time);
        $month = [];
        if ($choice_time < $now_time) {
            for ($i = 1; $i <= $day; $i++) {
                //查询每日是否签到
                $eq = strlen(strval($i)) > 1 ? $i : '0' . $i;
                $sign_date = $date . '-' . $eq;
                $is_sign = UserSign::where(['uid' => $this->user->id, 'create_date' => $sign_date])->count();
                $month[] = [
                    'week' => getWeekTwo(date('w', strtotime($sign_date))),
                    'day' => $i,
                    'status' => $is_sign ? 1 : 0
                ];
            }
        } else {
            //今天
            $d = date('d');
            $month = [];
            for ($i = 1; $i <= $day; $i++) {
                //查询每日是否签到
                $eq = strlen(strval($i)) > 1 ? $i : '0' . $i;
                $sign_date = $date . '-' . $eq;
                //时间还没到
                if (date('Y-m') == $date) {
                    if ($i > $d) {
                        $month[] = [
                            'week' => getWeekTwo(date('w', strtotime($sign_date))),
                            'day' => $i,
                            'status' => 3
                        ];
                    } else {
                        $is_sign = UserSign::where(['uid' => $this->user->id, 'create_date' => $sign_date])->count();
                        $month[] = [
                            'week' => getWeekTwo(date('w', strtotime($sign_date))),
                            'day' => $i,
                            'status' => $is_sign ? 1 : 0
                        ];
                    }
                } else {
                    $month[] = [
                        'week' => getWeekTwo(date('w', strtotime($sign_date))),
                        'day' => $i,
                        'status' => 3
                    ];
                }
            }
        }

        $result = [
            'month' => $month,
            'sign_day' => $this->user->sign_day,
            'always_day' => $this->user->always_day,
            'is_day_sign' => $this->user->is_day_sign,
            'week' => ['日', '一', '二', '三', '四', '五', '六'],
            'date' => date('Y年m月', strtotime($date)),
            'reduce' => date('Y-m', strtotime('-1 month', strtotime($date))),
            'plus' => date('Y-m', strtotime("+1 month", strtotime($date))),
        ];
        View::assign($result);

        return View::fetch();
    }

    public function signSdd()
    {
        return json(UserSign::add($this->user));
    }
}